package com.tanhua.dubbo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.Settings;
import com.tanhua.dubbo.SettingsAPI;
import com.tanhua.dubbo.mapper.SettingsMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SettingsAPIImpl implements SettingsAPI {

    @Autowired
    private SettingsMapper settingsMapper;


    @Override
    public Settings fingByUserId(Long userId) {
        QueryWrapper<Settings> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return settingsMapper.selectOne(wrapper);
    }

    @Override
    public void saveOrUpdate(Long userId, Settings settings) {
        //1 根据userId查找设置信息
        QueryWrapper<Settings> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Settings one = settingsMapper.selectOne(wrapper);
        //2 找到--执行更新操作
        if (one!=null){
            one.setLikeNotification(settings.getLikeNotification());
            one.setPinglunNotification(settings.getPinglunNotification());
            one.setGonggaoNotification(settings.getGonggaoNotification());
            settingsMapper.updateById(one);
        }else {
            //3 没找到--执行插入操作
            settings.setUserId(userId);
            settingsMapper.insert(settings);
        }


    }
}
